﻿Imports Microsoft.Kinect

Class MainWindow
    Dim sensor As KinectSensor
    Dim PixelData() As Byte

    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        sensor = Microsoft.Kinect.KinectSensor.KinectSensors(0)
        sensor.ColorStream.Enable()

        AddHandler sensor.ColorFrameReady, AddressOf sensor_VideoFrameReady

        sensor.Start()
    End Sub

    Private Sub Window_Unoaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Unloaded
        sensor.Stop()
    End Sub

    Private Sub sensor_VideoFrameReady(sender As Object, e As ColorImageFrameReadyEventArgs)
        Dim ReceivedData As Boolean = False

        Using CFrame As ColorImageFrame = e.OpenColorImageFrame()
            If CFrame Is Nothing Then
                ' The image processing took too long. More than 2 frames behind.
            Else
                ReDim PixelData(CFrame.PixelDataLength - 1)
                CFrame.CopyPixelDataTo(PixelData)
                ReceivedData = True
            End If
        End Using

        If ReceivedData Then
            Dim source As BitmapSource = BitmapSource.Create(640, 480, 96, 96,
    PixelFormats.Bgr32, Nothing, PixelData, 640 * 4)

            videoImage.Source = source
        End If
    End Sub
End Class